package com.sirdc.modules.sys.web.index;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.EarlyWarningDatabean;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.EarlyWarning;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.EarlyWarnFilter;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.*;
import net.huashitong.Indicators.utils.Comparators;
import net.huashitong.Indicators.utils.ComparatorsUtils;
import net.huashitong.Indicators.utils.ThiTitleUtils;
import net.huashitong.user.entity.Collection;
import net.huashitong.user.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;

/**
 * 首页预警数据
 */
@Controller
@RequestMapping("/index/earlyWarning")
public class IndexEarlyWarningController extends JsonBaseController {
    @Autowired
    private EarlyWarningService earlyWarningService;
    @Autowired
    private ConstructionIndustryService constructionIndustryService;
    @Autowired
    private GaugeIndustryService gaugeIndustryService;
    @Autowired
    private SocialZeroService socialZeroService;
    @Autowired
    private TertiaryIndustryService industryService;
    @Autowired
    private IndicatorBreakdownService breakdownService;
    @Autowired
    private CompletionClassificationService classificationService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private TertiaryIndustryService tertiaryIndustryService;


    @Override
    protected String getView(String arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    /**
     * 获取预警数据列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryEarlyWarning")
    public Message queryEarlyWarning(){
        List<EarlyWarning> earlyWarnings = earlyWarningService.query();
        List<EarlyWarningDatabean> databeans = new ArrayList<EarlyWarningDatabean>();
        for (EarlyWarning earlyWarning : earlyWarnings) {
            EarlyWarningDatabean databean = new EarlyWarningDatabean();
            TertiaryIndustry tertiaryIndustry=tertiaryIndustryService.queryBySysId(earlyWarning.getIndicatorId());
            databean.setTitle(earlyWarning.getTitle());
            databean.setSysId(earlyWarning.getSysId());
            databean.setDate(earlyWarning.getCreateDate());
            databean.setLeadership(tertiaryIndustry.getLeadership());
            databean.setUnitsNumber(tertiaryIndustry.getUnitsNumber());
            databeans.add(databean);
        }
        return coverMessage("200", "获取成功", databeans);
    }

    /**
     * 获取预警数据详细
     * @param filter
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping("/getEarlyWarningPhoto")
    public Message getEarlyWarningPhoto(@RequestBody EarlyWarnFilter filter) throws ParseException {
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        EarlyWarning earlyWarning = earlyWarningService.getById(filter.getSysId());
        IndicatorBreakdownFilter breakdownFilter = new IndicatorBreakdownFilter();
        breakdownFilter.setDate(earlyWarning.getDate());
        breakdownFilter.setCurrentMonth("201706");
        breakdownFilter.setArea(earlyWarning.getArea());
        breakdownFilter.setIndustryName(earlyWarning.getIndustryName());
        breakdownFilter.setIndustryId(earlyWarning.getIndicatorId());
        if(earlyWarning.getChartPosition().equals("5")){
            if(earlyWarning.getTitle().contains("固定资产投资乡镇")){
                maps = getGTXZ(breakdownFilter);
            }else if(earlyWarning.getTitle().contains("固定资产投资部门")){
                maps = getGTBM(breakdownFilter);
            }else if(earlyWarning.getTitle().contains("建筑业增加值企业")){
                maps = getJZYQY(breakdownFilter);
            }else if(earlyWarning.getTitle().contains("社会消费品零售总额乡镇")){
                maps = getSLXZ(breakdownFilter);
            }
        }else if(earlyWarning.getChartPosition().equals("7")){
            if(earlyWarning.getTitle().contains("固定资产投资企业")){
                maps = getGTQY(breakdownFilter);
            }
        }else if(earlyWarning.getChartPosition().equals("10")){
            if(earlyWarning.getTitle().contains("规模以上工业增加值行业")){
                maps = getGSHY(breakdownFilter);
            }
        }
        return coverMessage("200", "数据获取成功", maps);
    }

    /**
     * 固定资产投资总额各单位完成情况
     * @param breakdownFilter
     * @return
     */
    private List<Map<String, Object>> getGTQY(IndicatorBreakdownFilter breakdownFilter) {
        breakdownFilter.setQy("企业");
        List<CompletionClassification> classificationds = classificationService.queryByDWValue(breakdownFilter);
        List<CompletionClassificationDatabean> classificationDatabeands = new ArrayList<CompletionClassificationDatabean>();
        for (CompletionClassification completionClassification : classificationds) {
            CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
            classification.setActualCompletionValue(completionClassification.getActualCompletionValue());
            classification.setPlanneValue(completionClassification.getPlanneValue());
            Double doubleds1 = Double.parseDouble(completionClassification.getActualCompletionValue());
            Double doubleds2 = Double.parseDouble(completionClassification.getPlanneValue());
            Double doubleds3 = doubleds1/doubleds2*100;
            DecimalFormat df=new DecimalFormat(".##");
            if(doubleds3<50){
                classification.setYujz("0");
            }else {
                classification.setYujz("1");
            }
            if(doubleds3 == 0){
                classification.setCompleteAnnual(Double.toString(doubleds3));
            }else {
                String st=df.format(doubleds3);
                classification.setCompleteAnnual(st);
            }
            classification.setCategoryDetails(completionClassification.getCategoryDetails());
            classificationDatabeands.add(classification);
        }
        Collections.sort(classificationDatabeands, Comparators.getComparator());
        List<CompletionClassificationDatabean> classificationDatabeands2 = new ArrayList<CompletionClassificationDatabean>();
        List<CompletionClassificationDatabean> classificationDatabeands3 = new ArrayList<CompletionClassificationDatabean>();
        for (int i = 0; i < classificationDatabeands.size(); i++) {
            int y = classificationDatabeands.size();
            if(i == 6){
                break;
            }
            classificationDatabeands2.add(classificationDatabeands.get(i));
            classificationDatabeands3.add(classificationDatabeands.get(y-i-1));
        }
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        //类别详细
        List<String> categoryDetailsList=new ArrayList<String>();
        //实际完成值
        List<String> actualCompletionValueList=new ArrayList<String>();
        //计划值
        List<String>  planneValueList=new ArrayList<String>();
        //完成年计划
        List<String>  completeAnnualList=new ArrayList<String>();
        //是否预警列表
        List<String>  yujzList=new ArrayList<String>();
        for(CompletionClassificationDatabean classification:classificationDatabeands2){
            categoryDetailsList.add(classification.getCategoryDetails());
            actualCompletionValueList.add(classification.getActualCompletionValue());
            planneValueList.add(classification.getPlanneValue());
            completeAnnualList.add(classification.getCompleteAnnual());
            yujzList.add(classification.getYujz());
        }
        for(CompletionClassificationDatabean classification:classificationDatabeands3){
            categoryDetailsList.add(classification.getCategoryDetails());
            actualCompletionValueList.add(classification.getActualCompletionValue());
            planneValueList.add(classification.getPlanneValue());
            completeAnnualList.add(classification.getCompleteAnnual());
            yujzList.add(classification.getYujz());
        }
        map.put("categoryDetailsList",categoryDetailsList);
        map.put("actualCompletionValueList",actualCompletionValueList);
        map.put("planneValueList",planneValueList);
        map.put("completeAnnualList",completeAnnualList);
        map.put("yujzList",yujzList);
        map.put("unit", "万元");
        map.put("secTitle", "固定资产投资总额各单位完成情况");
        maps.add(map);
        return maps;
    }

    /**
     * 社会消费品零售总额各乡镇运行情况
     * @param breakdownFilter
     * @return
     */
    private List<Map<String, Object>> getSLXZ(
            IndicatorBreakdownFilter breakdownFilter) {
        breakdownFilter.setXz("乡镇");
        List<CompletionClassificationDatabean> classificationDatabean2s = socialZeroService.queryByXZValue(breakdownFilter);
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        //类别详细
        List<String> categoryDetailsList=new ArrayList<String>();
        //实际完成值
        List<String> actualCompletionValueList=new ArrayList<String>();
        //计划值
        List<String>  planneValueList=new ArrayList<String>();
        //完成年计划
        List<String>  completeAnnualList=new ArrayList<String>();
        //是否预警列表
        List<String>  yujzList=new ArrayList<String>();
        for(CompletionClassificationDatabean classification:classificationDatabean2s){
            categoryDetailsList.add(classification.getCategoryDetails());
            actualCompletionValueList.add(classification.getActualCompletionValue());
            planneValueList.add(classification.getPlanneValue());
            completeAnnualList.add(classification.getCompleteAnnual());
            yujzList.add(classification.getYujz());
        }
        map.put("categoryDetailsList",categoryDetailsList);
        map.put("actualCompletionValueList",actualCompletionValueList);
        map.put("planneValueList",planneValueList);
        map.put("completeAnnualList",completeAnnualList);
        map.put("yujzList",yujzList);
        map.put("unit", "万元");
        map.put("secTitle", "社会消费品零售总额各乡镇运行情况");
        maps.add(map);
        return maps;
    }

    /**
     * 规模以上工业增加值各行业完成情况
     * @param breakdownFilter
     * @return
     * @throws ParseException
     */
    private List<Map<String, Object>> getGSHY(IndicatorBreakdownFilter breakdownFilter) throws ParseException {
        breakdownFilter.setHy("行业");
        List<CompletionClassificationDatabean> classificationDatabean2s = gaugeIndustryService.queryByHYValue(breakdownFilter);
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        //类别详细
        List<String> categoryDetailsList=new ArrayList<String>();
        //实际完成值
        List<String> actualCompletionValueList=new ArrayList<String>();
        //计划值
        List<String>  planneValueList=new ArrayList<String>();
        //完成年计划
        List<String>  completeAnnualList=new ArrayList<String>();
        //是否预警列表
        List<String>  yujzList=new ArrayList<String>();
        for(CompletionClassificationDatabean classification:classificationDatabean2s){
            categoryDetailsList.add(classification.getCategoryDetails());
            actualCompletionValueList.add(classification.getActualCompletionValue());
            planneValueList.add(classification.getPlanneValue());
            completeAnnualList.add(classification.getCompleteAnnual());
            yujzList.add(classification.getYujz());
        }
        map.put("categoryDetailsList",categoryDetailsList);
        map.put("actualCompletionValueList",actualCompletionValueList);
        map.put("planneValueList",planneValueList);
        map.put("completeAnnualList",completeAnnualList);
        map.put("yujzList",yujzList);
        map.put("unit", "万元");
        map.put("secTitle", "规模以上工业增加值各行业完成情况");
        maps.add(map);
        return maps;
    }

    /**
     * 建筑业总产值前20家企业运行情况
     * @param breakdownFilter
     * @return
     */
    private List<Map<String, Object>> getJZYQY(IndicatorBreakdownFilter breakdownFilter) {
        breakdownFilter.setQy("企业");
        List<CompletionClassificationDatabean> classifications = constructionIndustryService.queryByDWValueSec(breakdownFilter);
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        //类别详细
        List<String> categoryDetailsList=new ArrayList<String>();
        //实际完成值
        List<String> actualCompletionValueList=new ArrayList<String>();
        //计划值
        List<String>  planneValueList=new ArrayList<String>();
        //完成年计划
        List<String>  completeAnnualList=new ArrayList<String>();
        //是否预警列表
        List<String>  yujzList=new ArrayList<String>();
        for(CompletionClassificationDatabean classification:classifications){
            categoryDetailsList.add(classification.getCategoryDetails());
            actualCompletionValueList.add(classification.getActualCompletionValue());
            planneValueList.add(classification.getPlanneValue());
            completeAnnualList.add(classification.getCompleteAnnual());
            yujzList.add("1");
        }
        map.put("categoryDetailsList",categoryDetailsList);
        map.put("actualCompletionValueList",actualCompletionValueList);
        map.put("planneValueList",planneValueList);
        map.put("completeAnnualList",completeAnnualList);
        map.put("yujzList",yujzList);
        map.put("unit", "亿元");
        map.put("data", classifications);
        map.put("secTitle", "建筑业总产值前20家企业运行情况");
        maps.add(map);
        return maps;
    }

    /**
     * 固定资产投资总额前十大部门累计完成情况
     * @param breakdownFilter
     * @return
     */
    private List<Map<String, Object>> getGTBM(IndicatorBreakdownFilter breakdownFilter) {
        //部门累计完成情况
        breakdownFilter.setBm("部门");
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        List<CompletionClassification> classifications = classificationService.queryByValue(breakdownFilter);
        List<CompletionClassificationDatabean> classificationDatabeans = new ArrayList<CompletionClassificationDatabean>();
        for (CompletionClassification completionClassification : classifications) {
            CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
            if(completionClassification.getActualCompletionValue().length()>4&&completionClassification.getPlanneValue().length()>4){
                classification.setActualCompletionValue(completionClassification.getActualCompletionValue().substring(0, completionClassification.getActualCompletionValue().length()-4)+
                        "."+completionClassification.getActualCompletionValue().substring(completionClassification.getActualCompletionValue().length()-4, completionClassification.getActualCompletionValue().length()-3));
                classification.setPlanneValue(completionClassification.getPlanneValue().substring(0, completionClassification.getPlanneValue().length()-4)+
                        "."+completionClassification.getPlanneValue().substring(completionClassification.getPlanneValue().length()-4, completionClassification.getPlanneValue().length()-3));
            }else {
                continue;
            }
            Double doublebs1 = Double.parseDouble(completionClassification.getActualCompletionValue());
            Double doublebs2 = Double.parseDouble(completionClassification.getPlanneValue());
            Double doublebs3 = doublebs1/doublebs2*100;
            DecimalFormat df=new DecimalFormat(".##");
            if(doublebs3<50){
                classification.setYujz("0");
            }else {
                classification.setYujz("1");
            }
            String st=df.format(doublebs3);

            classification.setCompleteAnnual(st);
            classification.setCategoryDetails(completionClassification.getCategoryDetails());
            classificationDatabeans.add(classification);
        }
        Collections.sort(classificationDatabeans, new ComparatorsUtils());
        List<CompletionClassificationDatabean> classificationDatabeans2 = new ArrayList<CompletionClassificationDatabean>();
        int k = 10;
        if(classificationDatabeans.size()<10){
            k = classificationDatabeans.size();
        }
        for (int i = 0; i < k; i++) {
            classificationDatabeans2.add(classificationDatabeans.get(i));
        }
        Map<String, Object> map = new HashMap<String, Object>();
        //类别详细
        List<String> categoryDetailsList=new ArrayList<String>();
        //实际完成值
        List<String> actualCompletionValueList=new ArrayList<String>();
        //计划值
        List<String>  planneValueList=new ArrayList<String>();
        //完成年计划
        List<String>  completeAnnualList=new ArrayList<String>();
        //是否预警列表
        List<String>  yujzList=new ArrayList<String>();
        for(CompletionClassificationDatabean classification:classificationDatabeans2){
            categoryDetailsList.add(classification.getCategoryDetails());
            actualCompletionValueList.add(classification.getActualCompletionValue());
            planneValueList.add(classification.getPlanneValue());
            completeAnnualList.add(classification.getCompleteAnnual());
            yujzList.add(classification.getYujz());
        }
        map.put("categoryDetailsList",categoryDetailsList);
        map.put("actualCompletionValueList",actualCompletionValueList);
        map.put("planneValueList",planneValueList);
        map.put("completeAnnualList",completeAnnualList);
        map.put("yujzList",yujzList);

        map.put("unit", "亿元");
        map.put("secTitle", "固定资产投资总额前十大部门累计完成情况");
        maps.add(map);
        return maps;
    }

    /**
     * 分乡镇50万元以上固定资产投资总额完成情况
     * @param breakdownFilter
     * @return
     */
    private List<Map<String, Object>> getGTXZ(IndicatorBreakdownFilter breakdownFilter) {
        breakdownFilter.setXz("乡镇");
        List<CompletionClassification> classification2s = classificationService.queryByXZValue(breakdownFilter);
        List<CompletionClassificationDatabean> classificationDatabean2s = new ArrayList<CompletionClassificationDatabean>();
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        for (CompletionClassification completionClassification : classification2s) {
            CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
            classification.setActualCompletionValue(completionClassification.getActualCompletionValue());
            classification.setPlanneValue(completionClassification.getPlanneValue());
            classification.setCategoryDetails(completionClassification.getCategoryDetails());
            Double doublebs1 = Double.parseDouble(completionClassification.getActualCompletionValue());
            Double doublebs2 = Double.parseDouble(completionClassification.getPlanneValue());
            Double doublebs3 = doublebs1/doublebs2*100;
            DecimalFormat df=new DecimalFormat("#.##");
            if(doublebs3<50){
                classification.setYujz("0");
            }else {
                classification.setYujz("1");
            }
            String st=df.format(doublebs3);
            if("0".equals(completionClassification.getActualCompletionValue())){
                classification.setCompleteAnnual("0");
            }else {
                classification.setCompleteAnnual(st);
            }
            classificationDatabean2s.add(classification);
        }
        Collections.sort(classificationDatabean2s, new ComparatorsUtils());
        Map<String, Object> map = new HashMap<String, Object>();
        //类别详细
        List<String> categoryDetailsList=new ArrayList<String>();
        //实际完成值
        List<String> actualCompletionValueList=new ArrayList<String>();
        //计划值
        List<String>  planneValueList=new ArrayList<String>();
        //完成年计划
        List<String>  completeAnnualList=new ArrayList<String>();
        //是否预警列表
        List<String>  yujzList=new ArrayList<String>();
        for(CompletionClassificationDatabean classification:classificationDatabean2s){
            categoryDetailsList.add(classification.getCategoryDetails());
            actualCompletionValueList.add(classification.getActualCompletionValue());
            planneValueList.add(classification.getPlanneValue());
            completeAnnualList.add(classification.getCompleteAnnual());
            yujzList.add(classification.getYujz());
        }

        map.put("categoryDetailsList",categoryDetailsList);
        map.put("actualCompletionValueList",actualCompletionValueList);
        map.put("planneValueList",planneValueList);
        map.put("completeAnnualList",completeAnnualList);
        map.put("yujzList",yujzList);
        map.put("unit", "万元");
        map.put("secTitle", "分乡镇50万元以上固定资产投资总额完成情况");
        maps.add(map);
        return maps;
    }
}
